public class Solution160 {
    /**
     * 编写一个程序，找到两个单链表相交的起始节点。
     */

    public ListNode getIntersectionNode(ListNode headA, ListNode headB) {
        ListNode n1 = headA;
        ListNode n2 = headB;
        int len1 = 0;
        int len2 = 0;
        while (n1 != null) {
            len1++;
            n1 = n1.next;
        }
        while (n2 != null) {
            len2++;
            n2 = n2.next;
        }
        if (len1 > len2) {
            for (int i = 0; i < len1 - len2; i++) {
                headA = headA.next;
            }
        }else {
            for (int i = 0; i < len2 - len1; i++) {
                headB = headB.next;
            }
        }
        while (headA != null && headB != null){
            if (headA == headB) return headA;
            headA = headA.next;
            headB = headB.next;
        }
        return null;
    }
}
